/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AlanTuringBombSimulator.java
 *
 * Created on 12-giu-2009, 10.27.10
 */

package turingbombsimulator;

/**
 *
 * @author francescoburato
 */
public class AlanTuringBombSimulator extends javax.swing.JFrame {

    /** Creates new form AlanTuringBombSimulator */
    private static String intArrayToString(int[] vett){
		String res = "";
		for(int i = 0; i < vett.length - 1; ++i)
			res = res + vett[i] + ",";
		return res + vett[vett.length-1];
	}
	private void setValues(){
		this.ComboScamb.removeAllItems();
		int[] base = {0,1,2,3,4};
		for(int i = 0; i < 60; ++i){
			this.ComboScamb.addItem(intArrayToString(AlanTuringBombs.disposizioni(base, 3, i)));
		}
	}
	public AlanTuringBombSimulator() {
		this.isInit = false;
        initComponents();
		this.setValues();
		this.isInit = true;
		this.theBomb = null;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
     private void initComponents() {

          jPanel1 = new javax.swing.JPanel();
          jLabel1 = new javax.swing.JLabel();
          jSeparator1 = new javax.swing.JSeparator();
          jScrollPane1 = new javax.swing.JScrollPane();
          CribText = new javax.swing.JTextArea();
          jLabel2 = new javax.swing.JLabel();
          jSeparator2 = new javax.swing.JSeparator();
          jScrollPane2 = new javax.swing.JScrollPane();
          CodedCribText = new javax.swing.JTextArea();
          FindButton = new javax.swing.JButton();
          jPanel2 = new javax.swing.JPanel();
          jLabel3 = new javax.swing.JLabel();
          jSeparator4 = new javax.swing.JSeparator();
          jLabel4 = new javax.swing.JLabel();
          ComboScamb = new javax.swing.JComboBox();
          jSeparator5 = new javax.swing.JSeparator();
          jLabel5 = new javax.swing.JLabel();
          jScrollPane3 = new javax.swing.JScrollPane();
          ListAssetti = new javax.swing.JList();
          Sequence = new javax.swing.JTextField();
          jSeparator3 = new javax.swing.JSeparator();

          setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

          jLabel1.setFont(new java.awt.Font("Lucida Grande", 1, 13)); // NOI18N
          jLabel1.setText("Crib in chiaro");

          CribText.setColumns(20);
          CribText.setLineWrap(true);
          CribText.setRows(5);
          jScrollPane1.setViewportView(CribText);

          jLabel2.setFont(new java.awt.Font("Lucida Grande", 1, 13)); // NOI18N
          jLabel2.setText("Testo cifrato");

          CodedCribText.setColumns(20);
          CodedCribText.setLineWrap(true);
          CodedCribText.setRows(5);
          jScrollPane2.setViewportView(CodedCribText);

          FindButton.setText("Trova");
          FindButton.addActionListener(new java.awt.event.ActionListener() {
               public void actionPerformed(java.awt.event.ActionEvent evt) {
                    FindButtonActionPerformed(evt);
               }
          });

          org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
          jPanel1.setLayout(jPanel1Layout);
          jPanel1Layout.setHorizontalGroup(
               jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
               .add(jPanel1Layout.createSequentialGroup()
                    .add(jLabel1)
                    .addContainerGap(193, Short.MAX_VALUE))
               .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)
               .add(jSeparator2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)
               .add(jPanel1Layout.createSequentialGroup()
                    .add(jLabel2)
                    .addContainerGap())
               .add(jPanel1Layout.createSequentialGroup()
                    .add(89, 89, 89)
                    .add(FindButton)
                    .addContainerGap(116, Short.MAX_VALUE))
               .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)
               .add(jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)
          );
          jPanel1Layout.setVerticalGroup(
               jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
               .add(jPanel1Layout.createSequentialGroup()
                    .add(jLabel1)
                    .add(2, 2, 2)
                    .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                    .add(jLabel2)
                    .add(6, 6, 6)
                    .add(jSeparator2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(FindButton))
          );

          jLabel3.setFont(new java.awt.Font("Lucida Grande", 1, 13)); // NOI18N
          jLabel3.setText("Soluzioni");

          jLabel4.setFont(new java.awt.Font("Lucida Grande", 1, 13)); // NOI18N
          jLabel4.setText("Configurazione Scambiatori");

          ComboScamb.addActionListener(new java.awt.event.ActionListener() {
               public void actionPerformed(java.awt.event.ActionEvent evt) {
                    ComboScambActionPerformed(evt);
               }
          });

          jLabel5.setFont(new java.awt.Font("Lucida Grande", 1, 13)); // NOI18N
          jLabel5.setText("Assetti Validi");

          jScrollPane3.setViewportView(ListAssetti);

          Sequence.setEnabled(false);

          org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
          jPanel2.setLayout(jPanel2Layout);
          jPanel2Layout.setHorizontalGroup(
               jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
               .add(jPanel2Layout.createSequentialGroup()
                    .add(jLabel3)
                    .addContainerGap(307, Short.MAX_VALUE))
               .add(jSeparator4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 368, Short.MAX_VALUE)
               .add(jPanel2Layout.createSequentialGroup()
                    .add(jLabel4)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(ComboScamb, 0, 159, Short.MAX_VALUE)
                    .addContainerGap())
               .add(jSeparator5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 368, Short.MAX_VALUE)
               .add(jPanel2Layout.createSequentialGroup()
                    .add(jLabel5)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(Sequence, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 270, Short.MAX_VALUE))
               .add(jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 368, Short.MAX_VALUE)
          );
          jPanel2Layout.setVerticalGroup(
               jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
               .add(jPanel2Layout.createSequentialGroup()
                    .add(jLabel3)
                    .add(2, 2, 2)
                    .add(jSeparator4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                         .add(jLabel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 18, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                         .add(ComboScamb, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(jSeparator5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                         .add(jLabel5)
                         .add(Sequence, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE))
          );

          jSeparator3.setOrientation(javax.swing.SwingConstants.VERTICAL);

          org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
          getContentPane().setLayout(layout);
          layout.setHorizontalGroup(
               layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
               .add(layout.createSequentialGroup()
                    .addContainerGap()
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(jSeparator3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
          );
          layout.setVerticalGroup(
               layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
               .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                    .addContainerGap()
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                         .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                         .add(org.jdesktop.layout.GroupLayout.LEADING, jSeparator3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 340, Short.MAX_VALUE)
                         .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap())
          );

          pack();
     }// </editor-fold>//GEN-END:initComponents

	private void FindButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FindButtonActionPerformed
		// TODO add your handling code here:
		this.CribText.setText(enigmasimulator.core.Enigma.enigmaClearTextFormatter(this.CribText.getText()));
		this.CodedCribText.setText(enigmasimulator.core.Enigma.enigmaClearTextFormatter(this.CodedCribText.getText()).toUpperCase());
		try{
		this.theBomb = new AlanTuringBombs(this.CribText.getText(),this.CodedCribText.getText(),0);
		}catch(BadCribException e){
			this.theBomb = null;
			javax.swing.JOptionPane.showMessageDialog(this, "Crib non valido");
			String [] res = {""};
			this.ListAssetti.setListData(res);
			this.Sequence.setText("");
		}
		if(this.theBomb != null){
			this.theBomb.startThemAll();
			String[] results = this.theBomb.getSolverResult(this.ComboScamb.getSelectedIndex());
			//this.ListAssetti.removeAll();
			this.ListAssetti.setListData(results);
			this.Sequence.setText(this.theBomb.getSequence());
		}
	}//GEN-LAST:event_FindButtonActionPerformed

	private void ComboScambActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ComboScambActionPerformed
		// TODO add your handling code here:
		if(this.isInit && this.theBomb !=null){
			String[] results = this.theBomb.getSolverResult(this.ComboScamb.getSelectedIndex());
			//this.ListAssetti.removeAll();
			this.ListAssetti.setListData(results);
		}
	}//GEN-LAST:event_ComboScambActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AlanTuringBombSimulator().setVisible(true);
            }
        });
    }
	private AlanTuringBombs theBomb;
	private boolean isInit;
     // Variables declaration - do not modify//GEN-BEGIN:variables
     private javax.swing.JTextArea CodedCribText;
     private javax.swing.JComboBox ComboScamb;
     private javax.swing.JTextArea CribText;
     private javax.swing.JButton FindButton;
     private javax.swing.JList ListAssetti;
     private javax.swing.JTextField Sequence;
     private javax.swing.JLabel jLabel1;
     private javax.swing.JLabel jLabel2;
     private javax.swing.JLabel jLabel3;
     private javax.swing.JLabel jLabel4;
     private javax.swing.JLabel jLabel5;
     private javax.swing.JPanel jPanel1;
     private javax.swing.JPanel jPanel2;
     private javax.swing.JScrollPane jScrollPane1;
     private javax.swing.JScrollPane jScrollPane2;
     private javax.swing.JScrollPane jScrollPane3;
     private javax.swing.JSeparator jSeparator1;
     private javax.swing.JSeparator jSeparator2;
     private javax.swing.JSeparator jSeparator3;
     private javax.swing.JSeparator jSeparator4;
     private javax.swing.JSeparator jSeparator5;
     // End of variables declaration//GEN-END:variables

}
